package controller.menu;

import controller.Operation.*;
import model.AdminUser;
import view.menu.AdminUserMenuView;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Question AdminUserMenuView
 * @Date 2022/12/17 20:01
 * @Solution
 */
public class AdminUserMenu implements ActionListener {
    public AdminUserMenu(AdminUser user) {
        this.user = user;
    }

    AdminUser user = new AdminUser();
    private JTable bookTable;
    private JTable userTable;
    private JTable recordTable;
    private DefaultTableModel userModel;
    private DefaultTableModel bookModel;
    private DefaultTableModel recordModel;
    private JButton deleteBookButton;
    private JButton deleteUserButton;
    private JButton showAllBookButton;
    private JButton showAllUserButton;
    private JButton reviseBookButton;
    private JButton reviseUserButton;
    private JButton insertBookButton;
    private JButton searchBookButton;
    private JButton searchUserButton;
    private JRadioButton isIn;
    private JPanel bookContainer;
    private JPanel userContainer;
    private JTextField bookId;
    private JTextField bookName;
    private JTextField bookAuthor;
    private JTextField bookPress;
    private JTextField userId;
    private JTextField userName;
    private JTextField userPassword;
    public void init() {
        AdminUserMenuView menu = new AdminUserMenuView();
        //获取各组件
        bookTable=menu.getBookTable();
        userTable=menu.getUserTable();
        recordTable=menu.getRecordTable();
        bookContainer=menu.getBookManager();
        userContainer=menu.getUserManager();
        deleteBookButton=menu.getBookDelete();
        deleteUserButton=menu.getUserDelete();
        showAllBookButton=menu.getBookShow();
        showAllUserButton=menu.getUserShow();
        reviseBookButton=menu.getBookRevise();
        reviseUserButton=menu.getUserRevise();
        insertBookButton=menu.getBookInsert();
        searchBookButton=menu.getBookSearch();
        searchUserButton=menu.getUserSearch();
        isIn=menu.getNoOut();
        bookId=menu.getBookId();
        bookName=menu.getBookName();
        bookAuthor=menu.getBookAuthor();
        bookPress=menu.getBookPre();
        userId=menu.getId();
        userName=menu.getUsername();
        userPassword=menu.getPassword();
        //初始化信息，显示全部
        bookModel=menu.getBookModel();
        userModel=menu.getUserModel();
        recordModel=menu.getRecordModel();
        Show.showAllBook(bookModel);
        Show.showAllUser(userModel);
        Show.showAllRecord(recordModel);
        menu.setVisible(true);
        //添加监听器
        //删除
        deleteBookButton.addActionListener(this);
        deleteUserButton.addActionListener(this);
        //显示全部
        showAllUserButton.addActionListener(this);
        showAllBookButton.addActionListener(this);
        //修改
        reviseBookButton.addActionListener(this);
        reviseUserButton.addActionListener(this);
        //新增
        insertBookButton.addActionListener(this);
        //查找
        searchBookButton.addActionListener(this);
        searchUserButton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==deleteBookButton||e.getSource()==deleteUserButton) {
            //确认删除
            int opt = JOptionPane.showConfirmDialog(bookContainer,
                    "请确认是否删除？", "确认删除",
                    JOptionPane.YES_NO_OPTION);
            if (opt == JOptionPane.YES_OPTION) {
                //确认继续操作
                if(e.getSource()==deleteBookButton) {
                    if (Delete.deleteBook(bookModel, bookTable) == 1) {
                        JOptionPane.showMessageDialog(bookContainer, "删除成功");
                    }
                } else {
                    if (Delete.deleteUser(userModel, userTable) == 1) {
                        JOptionPane.showMessageDialog(userContainer, "删除成功");
                    }
                }
            }
        } else if(e.getSource()==showAllBookButton) {
            Show.showAllBook(bookModel);
        } else if(e.getSource()==showAllUserButton) {
            Show.showAllUser(userModel);
        } else if(e.getSource()==reviseBookButton) {
            Revise.reviseBook(bookContainer,bookModel,bookTable,bookId,bookName,bookAuthor,bookPress);
        } else if(e.getSource()==insertBookButton) {
            Insert.insertBook(bookContainer,bookModel,bookTable,bookId,bookName,bookAuthor,bookPress);
        } else if(e.getSource()==searchBookButton) {
            Search.bookSearch(bookContainer,bookModel,bookTable,bookId,bookName,bookAuthor,bookPress,isIn);
        } else if(e.getSource()==reviseUserButton) {
            Revise.reviseUser(userContainer,userModel,userTable,userId,userName,userPassword);
        } else if(e.getSource()==searchUserButton) {
            Search.userSearch(userContainer,userModel,userTable,userId,userName,userPassword);
        }
    }
}
